﻿<Window x:Class="SEToolbox.Views.WindowLoad"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:commands="clr-namespace:SEToolbox.Services"
        xmlns:controls="clr-namespace:SEToolbox.Controls"
        xmlns:converters="clr-namespace:SEToolbox.Converters"
        ResizeMode="CanResize" ShowInTaskbar="False" WindowStartupLocation="CenterOwner" WindowStyle="ThreeDBorderWindow" Style="{StaticResource DefaultWindowStyle}"
        Title="Select World" Height="400" Width="700" MinWidth="260" MinHeight="200" MaxWidth="1200" MaxHeight="1024" Icon="/SEToolbox;component/app.ico" 
        commands:DialogService.IsRegisteredView="True"
        commands:DialogCloser.DialogResult="{Binding CloseResult}"
        FocusManager.FocusedElement="{Binding ElementName=uxWorldSelectionBox}">
    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
        <converters:NullImageConverter x:Key="NullImageConverter"/>
        <converters:BusyMouseConverter x:Key="BusyMouseConverter" />
        <converters:ResouceToImageConverter x:Key="ResouceToImageConverter"/>
        <CollectionViewSource Source="{Binding Worlds}" x:Key="GroupedItems">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="GroupDescription" />
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    </Window.Resources>
    <Window.Cursor>
        <Binding Path="IsBusy" Converter="{StaticResource BusyMouseConverter}"/>
    </Window.Cursor>
    <Window.InputBindings>
        <KeyBinding Key="F5" Command="{Binding RefreshCommand}" />
    </Window.InputBindings>
    <Grid>
        <Grid Margin="8">
            <Grid.ColumnDefinitions>
                <ColumnDefinition MinWidth="100" Width="6*"/>
                <ColumnDefinition Width="4"/>
                <ColumnDefinition MinWidth="98" MaxWidth="200"/>
            </Grid.ColumnDefinitions>
            <controls:SortableListView ItemsSource="{Binding Source={StaticResource GroupedItems}}" SelectionMode="Single" SelectedValue="{Binding SelectedWorld}" x:Name="uxWorldSelectionBox" VerticalAlignment="Stretch"
                                       ColumnHeaderArrowUpTemplate="{StaticResource HeaderTemplateArrowUp}" ColumnHeaderArrowDownTemplate="{StaticResource HeaderTemplateArrowDown}">
                <ListView.View>
                    <GridView>
                        <controls:SortableGridViewColumn SortBinding="{Binding SessionName}" Header="Name" Width="240">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding SessionName}" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis"/>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </controls:SortableGridViewColumn>
                        <GridViewColumn DisplayMemberBinding="{Binding LastLoadTime}" Header="Loaded"/>
                        <GridViewColumn DisplayMemberBinding="{Binding LastSaveTime}" Header="Saved"/>
                        <GridViewColumn DisplayMemberBinding="{Binding WorkshopId}" Header="Workshop"/>
                        <GridViewColumn DisplayMemberBinding="{Binding Version}" Header="Version"/>
                    </GridView>
                </ListView.View>
                <ListView.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.ContainerStyle>
                            <Style TargetType="{x:Type GroupItem}">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type GroupItem}">
                                            <Expander IsExpanded="True">
                                                <Expander.Header>
                                                    <DockPanel>
                                                        <StackPanel Orientation="Horizontal">
                                                            <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" Margin="5,0,0,0"/>
                                                        </StackPanel>
                                                    </DockPanel>
                                                </Expander.Header>
                                                <Expander.Content>
                                                    <ItemsPresenter/>
                                                </Expander.Content>
                                            </Expander>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </GroupStyle.ContainerStyle>
                    </GroupStyle>
                </ListView.GroupStyle>
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseDoubleClickItem">
                        <commands:CommandAction Command="{Binding LoadCommand}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
                <ListView.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Open Folder" Command="{Binding OpenFolderCommand}">
                            <MenuItem.Icon>
                                <Image Width="16" Height="16" Source="{Binding Converter={StaticResource ResouceToImageConverter}, ConverterParameter=folder}" />
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="View Workshop" Command="{Binding OpenWorkshopCommand}">
                            <MenuItem.Icon>
                                <Image Width="16" Height="16" Source="{Binding Converter={StaticResource ResouceToImageConverter}, ConverterParameter=workshop16}" />
                            </MenuItem.Icon>
                        </MenuItem>
                    </ContextMenu>
                </ListView.ContextMenu>
            </controls:SortableListView>
            <GridSplitter Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Center" Width="4"/>
            <Grid Grid.Column="2" Margin="8,0,0,0">
                <StackPanel VerticalAlignment="Top">
                    <Image HorizontalAlignment="Stretch" Source="{Binding Path=SelectedWorld.ThumbnailImageFilename, Converter={StaticResource NullImageConverter}}" Cursor="Hand" ToolTip="Click to Zoom in">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="MouseUp">
                                <commands:CommandAction Command="{Binding ZoomThumbnailCommand}"/>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </Image>
                </StackPanel>
                <StackPanel Orientation="Vertical" HorizontalAlignment="Right" VerticalAlignment="Bottom">
                    <Button Width="90" Command="{Binding RepairCommand}" Content="_Repair"  Margin="0,0,0,32" ToolTip="This will attempt to repair the save content it it finds any issues." />
                    <Button Width="90" Command="{Binding BrowseCommand}" Content="_Browse"  Margin="0,0,0,32" />
                    <Button Width="90" Command="{Binding LoadCommand}" Content="_Open" IsDefault="True" Margin="0,0,0,8" />
                    <Button Width="90" Command="{Binding CancelCommand}" Content="Cancel" IsCancel="True"/>
                </StackPanel>
            </Grid>
        </Grid>
        <Border Visibility="{Binding ZoomThumbnail, Converter={StaticResource BooleanToVisibilityConverter}}" Background="{DynamicResource {x:Static SystemColors.ControlDarkDarkBrushKey}}" Cursor="Hand" ToolTip="Click to Zoom out">
            <Image Source="{Binding Path=SelectedWorld.ThumbnailImageFilename, Converter={StaticResource NullImageConverter}}" IsHitTestVisible="False"/>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseUp">
                    <commands:CommandAction Command="{Binding ZoomThumbnailCommand}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Border>
        <controls:BusyIndicator Visibility="{Binding IsBusy, Converter={StaticResource BooleanToVisibilityConverter}}" />
    </Grid>
</Window>
